// pages/goodManager/goodManager.js
var httpClient = require('../../utils/httpClient.js');
var serverConstants = require('../../constants/serverConstants.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    relations: [],
    currentParentIndex: 0,
    goods: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.queryCatogery();
    this.queryGoods();
  },
  searchInput: function (e) {
    var detail = e.detail;
  },
  queryCatogery: function () {
    var page = this;
    var storge = wx.getStorageSync("catogeryRelations");
    if (!storge) {
      httpClient.get("/catogery/getAll", function (result) {
        wx.setStorageSync("catogeryRelations", result);
        var relations = [{ parentName: "全部", id: '0' }]
        relations = relations.concat(result);
        page.setData({
          relations: result
        })
      });
    } else {
      var relations = [{ parentName: "全部", id: '0' }]
      relations = relations.concat(storge);
      page.setData({
        relations: relations
      })
    }
  },
  //选择主类别
  changeParentStyle: function (e) {
    this.setData({
      currentParentIndex: e.currentTarget.dataset.index
    });
    var bigCatogery = this.data.relations[this.data.currentParentIndex].parentName
    if (this.data.relations[this.data.currentParentIndex].id == '0') {
      bigCatogery = null;
    }
    this.queryGoods(bigCatogery);
  },
  //选择子类别
  chooseCatogery: function (e) {
    this.queryGoods(null, e.currentTarget.dataset.item.id)
  },
  queryGoods: function (bigCatogery, catogeryId, goodName) {
    var that = this;
    httpClient.post("/good/findByCatogery",
      { "bigCatogery": bigCatogery, "catogeryId": catogeryId, "goodName": goodName, "shopId": wx.getStorageSync("shopId") },
      function (data) {
        for (var i in data) {
          data[i].picUrl = serverConstants.serverHost + data[i].picUrl;
        }
        that.setData({
          goods: data
        });
      });
  }
})